﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using BlackCoffeeRESTful.Configs;
using BCService.Utils;
using BCService.OA.BusinessFlowInstance;
using BCDto.MySql.BusinessFlowInstance;

namespace BlackCoffeeRESTful.Controllers.Management.OA.BusinessFlowInstance
{
    /// <summary>
    /// OA系统业务流实例
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class BusinessFlowInstanceController : ManagementBaseController
    {
        private readonly IBusinessFlowInstanceService businessFlowInstanceService;

        public BusinessFlowInstanceController(IBusinessFlowInstanceService businessFlowInstanceService)
        {
            this.businessFlowInstanceService = businessFlowInstanceService;
        }

        #region 添加OA系统业务流实例
        /// <summary>
        ///  添加OA系统业务流实例
        /// </summary>
        /// <param name="requestDto">OA系统业务流实例信息</param>
        /// <response code="201">添加OA系统业务流实例成功</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加OA系统业务流实例")]
        [ProducesResponseType(typeof(BusinessFlowInstanceDto), 201)]
        public IActionResult Post(
            [FromBody]BusinessFlowInstanceRequestDto requestDto)
        {
            var result = this.businessFlowInstanceService.Add(requestDto);
            return this.Created(string.Format("api/v1/sim/BusinessFlowInstance/{0}", result.BusinessFlowInstanceId), result);
        }
        #endregion

        #region 修改OA系统业务流实例
        /// <summary>
        ///  修改OA系统业务流实例
        /// </summary>
        /// <param name="businessFlowInstanceId">OA系统业务流实例Id</param>
        /// <param name="requestDto">OA系统业务流实例信息</param>
        /// <response code="200">修改OA系统业务流实例成功</response>
        /// <returns></returns>
        [HttpPut("{business_flow_instance_id}")]
        [Log("修改OA系统业务流实例")]
        [ProducesResponseType(typeof(BusinessFlowInstanceDto), 200)]
        public IActionResult Put([FromRoute(Name = "business_flow_instance_id")]long businessFlowInstanceId, [FromBody]BusinessFlowInstanceRequestDto requestDto)
        {
            BusinessFlowInstancePutDto putDto = requestDto.As<BusinessFlowInstancePutDto>();
            putDto.BusinessFlowInstanceId = businessFlowInstanceId;
            return this.Ok(this.businessFlowInstanceService.Update(putDto));
        }
        #endregion
        
        #region 获取OA系统业务流实例
        /// <summary>
        ///  获取OA系统业务流实例
        /// </summary>
        /// <param name="businessFlowInstanceId">OA系统业务流实例Id</param>
        /// <response code="200">获取OA系统业务流实例</response>
        /// <returns></returns>
        [HttpGet("{business_flow_instance_id}")]
        [ProducesResponseType(typeof(BusinessFlowInstanceDto), 200)]
        public IActionResult Get([FromRoute(Name = "business_flow_instance_id")]long businessFlowInstanceId)
        {
            return this.Ok(this.businessFlowInstanceService.GetEntity(businessFlowInstanceId));
        }
        #endregion

        #region 获取OA系统业务流实例列表
        /// <summary>
        ///  获取OA系统业务流实例列表
        /// </summary>
        /// <response code="200">获取OA系统业务流实例列表</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(BusinessFlowInstanceDto), 200)]
        public IActionResult Get()
        {
            return this.Ok(this.businessFlowInstanceService.GetList(this.Admin.CompanyId));
        }
        #endregion

        #region 获取OA系统业务流实例（分页）
        /// <summary>
        ///  获取OA系统业务流实例（分页）
        /// </summary>
        /// <param name="businessFlowModelId">业务流模型Id</param>
        /// <param name="businessFlowInstanceName">业务流实例名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">获取OA系统业务流实例（分页）</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<BusinessFlowInstanceDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "business_flow_model_id")]long? businessFlowModelId,
            [FromQuery(Name = "business_flow_instance_name")]string businessFlowInstanceName,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            int pageIndex = 1,
            int pageSize = 10)
        {
            return this.Ok(this.businessFlowInstanceService.Query(businessFlowModelId,businessFlowInstanceName, start, end, pageIndex, pageSize));
        }
        #endregion
    }
}

